package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.entity.XlsCartEntity;
import io.renren.common.entity.XlsUserEntity;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.dao.XlsCartDao;
import io.renren.dao.XlsCommodityDao;
import io.renren.dao.XlsUserDao;
import io.renren.dto.XlsCartDTO;
import io.renren.service.XlsCartService;
import io.renren.utils.AppResult;
import io.renren.utils.Convert;
import io.renren.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 购物车
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2020-04-11
 */
@Service
public class XlsCartServiceImpl extends CrudServiceImpl<XlsCartDao, XlsCartEntity, XlsCartDTO> implements XlsCartService {

    @Autowired
    private XlsCartDao xlsCartDao;
    @Autowired
    private XlsUserDao xlsUserDao;
    @Autowired
    private XlsCommodityDao xlsCommodityDao;

    @Override
    public QueryWrapper<XlsCartEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<XlsCartEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    @Transactional
    public AppResult addcarts(XlsCartEntity xlsCartEntity) {
        xlsCartEntity.setCreatTime(new Date());
        int insert = xlsCartDao.insert(xlsCartEntity);
        if (insert > 0) {
            return AppResult.OK("添加成功！", null);
        }
        return AppResult.ERROR("添加失败！");
    }

    @Override
    public AppResult getCartInfo(XlsCartEntity xlsCartEntity) {
        List<Map<String, Object>> map = xlsCartDao.getCartInfo(xlsCartEntity);
        return AppResult.OK(map);
    }

    @Override
    public AppResult updateCart(XlsCartEntity xlsCartEntity) {
        int i = xlsCartDao.updateById(xlsCartEntity);
        if (i > 0) {
            return AppResult.OK("成功");
        }
        return AppResult.ERROR("失败");
    }

    @Override
    public AppResult remove(String ids) {
        int i = xlsCartDao.deleteCartByIds(Convert.toStrArray(ids));
        if (i > 0) {
            return AppResult.OK("删除成功！");
        }
        return AppResult.ERROR("删除失败！");
    }

    @Override
    public AppResult gotoPay(Long userId) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> resultMap = new HashMap<>();
        XlsUserEntity users = xlsUserDao.selectById(userId);
        if (StringUtils.isNull(users)) {
            return AppResult.ERROR("无效的用户！");
        }
        XlsCartDTO cart = new XlsCartDTO();
        cart.setUserId(userId);
        cart.setIsCheck(1);
        List<XlsCartDTO> cartList = xlsCartDao.selectCartList(cart);
        if (cartList.isEmpty()) {
            return AppResult.ERROR("没有记录");
        }
        Set<Integer> retailSet = new HashSet<Integer>();
        Map<String, Object> retailCart = new HashMap<>();
        retailCart.put("goodsType", 1);
        XlsUserEntity xlsUserEntity = xlsUserDao.selectById(userId);
        cart.setUserId(xlsUserEntity.getId());
        List<Map<String, Object>> retailList = xlsCartDao.selectByCommodityUserId(cart);
        List<Map<String, Object>> goodsMapList = new ArrayList<>();
        for (Map<String, Object> retailMap : retailList) {
            Map<String, Object> goodsMap = new HashMap<>();
            goodsMap.put("goodsName", retailMap.get("goodsName").toString());
            goodsMap.put("marketPrice", retailMap.get("marketPrice").toString());
            goodsMap.put("shopPrice", retailMap.get("shopPrice").toString());
            if (StringUtils.isNotNull(retailMap.get("goodsImg"))) {
                String goodsImg = retailMap.get("goodsImg").toString();
                goodsMap.put("goodsImg", goodsImg);
            }
            goodsMap.put("cartNum", retailMap.get("cartNum").toString());
            goodsMap.put("isCheck", retailMap.get("isCheck").toString());
            goodsMap.put("cartId", retailMap.get("cartId").toString());
            goodsMap.put("goodsId", retailMap.get("goodsId").toString());
            goodsMap.put("goodsType", 1);
            goodsMapList.add(goodsMap);
        }
        retailCart.put("goodsMapList", goodsMapList);
        return AppResult.OK(retailCart);
    }
}